package multimedia.model;

import java.io.*;
import java.util.*;

import multimedia.model.colorspace.YIQ;
import multimedia.util.ProjectConstants;

/**
 * 
 * @author david
 *
 */
public class Histogram
{
	private Vector<String> histogramStrings;

    public Histogram(int imageID,  ImageCell imageCell) // 2 parameters for input: id of the image "imageID" and the 8x8 image cell "region"
	{
		File inFile  = new File(ProjectConstants.IMAGE_DB + "histogramSpecification.txt");  // File to read from.

		String[] array = new String[384];

		int[] binArray = new int[384];

		try
		{
			 BufferedReader reader = new BufferedReader(new FileReader(inFile));

			 String file = "";

			 //... Loop as long as there are input lines.
			 String line = null;
			 while ((line=reader.readLine()) != null)
			 {
				 file = file.concat(line);
			 }

			 array = file.split("/");


			 //... Close reader
			 reader.close();  // Close to unlock.

		}
		catch (IOException e)
		{
			System.err.println(e);
			System.exit(1);
		}

		for(int i = 0; i< 384; i++)
		{
			binArray[i] = Integer.parseInt(array[i]);
		}

		int binCounter = -1;


		Vector<Bins> binVector = new Vector<Bins>();

		for(int i = 0; i < 64; i++)
		{
			Bins bin = new Bins(i, binArray[++binCounter], binArray[++binCounter], binArray[++binCounter], binArray[++binCounter], binArray[++binCounter], binArray[++binCounter]);
			binVector.add(bin);
		}


		int[] histogramArray = new int[64];

		//initiate histogram Array to all 0's
		for(int i = 0; i < 64; i++)
			histogramArray[i] = 0;

		boolean temp;

		Bins tempBin = new Bins();

		int counter;

		PixelCoordinate pixMap = new PixelCoordinate(imageCell.getPixMap(), 8);

		for(int m = 0; m < 8; m++)
			for(int n = 0; n < 8; n++)
			{
				temp = true;
				counter = 0;
				
				YIQ currentPixel = new YIQ(pixMap.getPixelAt(m,n).getFirstComponent(),pixMap.getPixelAt(m,n).getSecondComponent(),pixMap.getPixelAt(m,n).getThirdComponent());
				int y_comp = (int) currentPixel.y;
				int i_comp = (int) currentPixel.i;
				int q_comp = (int) currentPixel.q;

				while(temp)
				{
				    //TODO: Crashes because counter causes the array to go out of bounds, idk what the
				    //purpose of it is. Fix.
					tempBin = binVector.get(counter);
					if(tempBin.minY <= y_comp && tempBin.maxY >= y_comp)
						if(tempBin.minI <= i_comp && tempBin.maxI >= i_comp)
							if(tempBin.minQ <= q_comp && tempBin.maxQ >= q_comp)
								{
									histogramArray[tempBin.bin]++;
									temp = false;
								}
					counter++;
				}
			}

		histogramStrings = new Vector<String>();
		
		for(int q = 0; q < 64; q++)
		{
			// imageID is the parameter passed to this class
			// region.cell_coord is the cell_coord variable from the image cell that was passed as a parameter
			histogramStrings.add(imageID + "/" + imageCell.getcellCoordinate().x + "," + imageCell.getcellCoordinate().y + "/" + q + "/" + histogramArray[q]);
		}
	}
    
    public Vector<String> getHistogramStrings()
    {
        return histogramStrings;
    }
}